---@class IDataModel : LuaObject
---@field protected super IDataModel
local this = BaseClass("IDataModel")

function this:__ctor()
    self.mDataCount = 0
    self.mItemDic = {}
    self.mItemList = {}
end

function this:GetDataById(id)
    local idx = self:GetIdxById(id)
    return self:GetDataByIdx(idx)
end

function this:GetDataByIdx(idx)
    return self.mItemList[idx]
end

function this:GetIdxById(id)
    return self.mItemDic[id]
end

function this:AddData(data)
    table.insert(self.mItemList, data)
    self.mDataCount = self.mDataCount + 1
    if (data and data.id) then
        self.mItemDic[data.id] = self.mDataCount
    end
end

function this:RemoveDataByIdx(idx)
    table.remove(self.mItemList, idx)
    self.mDataCount = self.mDataCount - 1

    local data = self:GetDataByIdx(idx)
    if (data and data.id) then
        self.mItemDic[data.id] = nil
    end
end

function this:RemoveDataById(id)
    local idx = self:GetIdxById(id)
    self:RemoveDataByIdx(idx)
end

IDataModel = this